package api

import (
	"github.com/labstack/echo/v4"
	"longmen/server/config/global"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/const"
	"longmen/server/pkg/common/helper/util"
	"math/rand"
	"time"
)

// @Summary 获取广告位
// @Description 获取广告位
// @Accept  json
// @Produce  json
// @Tags Ads
// @Param   sendBody body api_models.AdsTerminal true "body"
// @Success 200 {object} []api_models.AdsLocation "result"
// @Router /api/ads/getAdLocation [POST]
func (base *Controller) GetAdLocation(c echo.Context) error {
	var (
		err  error
		req  = &api_models.AdsTerminal{}
		resp = make([]api_models.AdsLocation, 8)
	)

	if err = util.Json2Struct(c.Get("body").(string), req); err != nil {
		return util.BadParam(c, err)
	}
	if err = _const.Validate.Struct(req); err != nil {
		global.Log.Errorf("6GetAdLocation9参数错误%v", err)
		return util.BadParam(c, err)
	}
	if req.Equipment < 1 || req.Equipment > 2 {
		return util.BadParam(c, nil)
	}

	// TODO terminal 转枚举类型判断 ....

	// 应该从数据库中获取，目前后端PHP未拆分，写死 （注意没有8）
	resp = []api_models.AdsLocation{
		{
			Category: 1,
			Title:    "启动页广告",
		},
		{
			Category: 2,
			Title:    "首页轮播广告",
		},
		{
			Category: 3,
			Title:    "首页弹窗广告",
		},
		{
			Category: 4,
			Title:    "动态列表广告",
		},
		{
			Category: 5,
			Title:    "短视频广告",
		},
		{
			Category: 6,
			Title:    "首页导航背景图",
		},
		{
			Category: 7,
			Title:    "首页宣传背景海报",
		},
		{
			Category: 9,
			Title:    "默认头像",
		},
	}

	return util.BuildSuccess(c, resp, "")
}

// @Summary 获取广告位图片
// @Description 获取广告位图片
// @Accept  json
// @Produce  json
// @Tags Ads
// @Param   sendBody body api_models.AdsReq true "body"
// @Success 200 {object} api_models.AdsResp "result"
// @Router /api/ads/getAds [POST]
func (base *Controller) GetAds(c echo.Context) error {
	var (
		err  error
		req  = &api_models.AdsReq{}
		resp = make([]*api_models.AdsResp, 0)
	)

	if err = util.Json2Struct(c.Get("body").(string), req); err != nil {
		return util.BadParam(c, err)
	}
	if err = _const.Validate.Struct(req); err != nil {
		global.Log.Errorf("6GetAds9参数错误%v", err)
		return util.BadParam(c, err)
	}
	if req.Equipment < 1 || req.Equipment > 2 {
		return util.BadParam(c, nil)
	}
	if resp, err = base.Service.GetAds(req.Equipment, req.Category); err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	if req.Category == 1 || req.Category == 14 { //启动页广告和直播间弹窗广告
		regSource := base.GetRegSource(c)
		if len(regSource) > 0 {
			_, ok := allowChannel[regSource]
			if !ok {
				resp = resp[0:0]
			} else {
				rand.Seed(time.Now().UnixNano())
				rand.Shuffle(len(resp), func(i, j int) {
					resp[i], resp[j] = resp[j], resp[i]
				})
			}
		}
	}
	return util.BuildSuccess(c, resp, "")
}

// 非官方pc|h5|app不弹出广告
var allowChannel = map[string]int{
	"android-official": 1,
	"H5OFFICIAL":       1,
	"OFFICIAL":         1,
	"NJRD561S":         1,
}

// @Summary APP启动页广告
// @Description APP启动页广告
// @Accept  json
// @Produce  json
// @Tags Ads
// @Success 200 {object} models_main.Ads "result"
// @Router /api/ads/getLaunchAd [POST]
func (base *Controller) GetLaunchAd(c echo.Context) error {
	resp, err := base.Service.GetLaunchAd()
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	regSource := base.GetRegSource(c)
	if len(regSource) > 0 {
		_, ok := allowChannel[regSource]
		if !ok {
			resp = nil
		}
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary APP首页轮播广告
// @Description APP首页轮播广告
// @Accept  json
// @Produce  json
// @Tags Ads
// @Success 200 {object} models_main.Ads "result"
// @Router /api/ads/getHomeScrollAd [POST]
func (base *Controller) GetHomeScrollAd(c echo.Context) error {
	resp, err := base.Service.GetHomeScrollAd()
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary APP首页弹窗广告
// @Description APP首页弹窗广告
// @Accept  json
// @Produce  json
// @Tags Ads
// @Success 200 {object} models_main.Ads "result"
// @Router /api/ads/getHomePopAd [POST]
func (base *Controller) GetHomePopAd(c echo.Context) error {
	resp, err := base.Service.GetHomePopAd()
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary WEB顶部导航背景图
// @Description WEB顶部导航背景图
// @Accept  json
// @Produce  json
// @Tags Ads
// @Success 200 {object} models_main.Ads "result"
// @Router /api/ads/getNavBg [POST]
func (base *Controller) GetNavBg(c echo.Context) error {
	resp, err := base.Service.GetNavBg()
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary WEB首页宣传大背景图
// @Description WEB首页宣传大背景图
// @Accept  json
// @Produce  json
// @Tags Ads
// @Success 200 {object} models_main.Ads "result"
// @Router /api/ads/getHomeBannerBg [POST]
func (base *Controller) GetHomeBannerBg(c echo.Context) error {
	resp, err := base.Service.GetHomeBannerBg()
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccessRaw(c, resp, "")
}
